﻿using UnityEngine;
using System.Collections;

public static class UnityHelpler
{
    public static Vector3 OArray2Vec3(object[] arr)
    {
        return new Vector3((float)arr[0],(float)arr[1],(float)arr[2]);
    }

    public static void AddChild(Transform c,Transform p)
    {
        c.parent = p;
        c.localPosition = Vector3.zero;
        c.localRotation = Quaternion.identity;
    }

    public static T GetComponentByIndex<T>(Transform t,int idx) where T : Component
    {
        int ti = 0;
        for (int i = 0; i < t.childCount;i++)
        {
            T component = t.GetChild(i).GetComponent<T>();
            if (component != null)
            {
                if (ti == idx) 
                {
                    return component;
                }
                ti++;
            }
        }
        return default(T);
    }

    public static void EachChildComponents<T>(Transform trans,System.Action<T,int> fn) where T: Component
    {
        for (int i = 0; i < trans.childCount;i++)
        {
            int addi = 0;
            T v = trans.GetChild(i).GetComponent<T>();
            if (v != null) 
            {
                if (fn != null) 
                {
                    fn(v,addi);
                }
                addi++;
            }
        }
    }
}
